﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Timers;



namespace 学生成绩查询及选课系统.student
{
    public partial class optionCourse : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                username.Text = Session["user"].ToString();
                
            }

        }



        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cno = ((Button)e.Item.FindControl("Button1")).CommandArgument.ToString();
            string cname = ((Label)e.Item.FindControl("cname")).Text;
            string tno = ((Label)e.Item.FindControl("tno")).Text;
            string tname = ((Label)e.Item.FindControl("tname")).Text;
            string cproperties = ((Label)e.Item.FindControl("cproperties")).Text;
            string cCount = ((Label)e.Item.FindControl("cCount")).Text;

            if (e.CommandName=="chooseCourse")
            {
                if (int.Parse(cCount)<=0)
                {
                    Label2.Text = "该选修课程数量已为0!请选择其他课程";
                  
                    //Response.Write("<script>alert('该选修课程数量已为0!请选择其他课程');</script>");
                    return;
                    //Repeater1.DataBind();
                    //return;
                }
                string sql_select = "select count(*) from grade where status=1 and stuno=@stuno and cno=@cno";
                SqlParameter[] SqlPArry = { new SqlParameter("@stuno", username.Text),
                                          new SqlParameter("@cno", cno) };

                string sql_count = "select count(*) from grade g join course c on g.cno=c.cno where cproperties='选修' and status=1  and g.stuno=@stuno";
                SqlParameter[] sqlarray2 = { new SqlParameter("@stuno", username.Text) };
                object num = DBAccess.GetOne(sql_count, sqlarray2);

                if ((int)num == 2)
                {
                    Label2.Text = "只能同时选修两门课，可尝试取消原选修课程，再进行选课";
                    //Response.Write("<script>alert('只能同时选修两门课，可尝试取消原选修课程，再进行选课');</srcipt>");
                    return;
                }

                object count = DBAccess.GetOne(sql_select, SqlPArry);
                if ((int)(count) != 0)
                {
                    Label2.Text = "这门课程你已经选了，请不要重复选择";
                    
                    //Response.Write("<script>alert('这门课程你已经选了，请不要重复选择');</srcipt>");
                    return;
                    //Repeater1.DataBind();
                }   
        
                SqlParameter[] SqlPArry3 = { new SqlParameter("@stuno", username.Text),
                                          new SqlParameter("@cno", cno) };
                //选择课程时,先判断数据库是否有数据，有则修改，没有则插入新数据,不等于0表示有结果
                int res = DBAccess.RunProcedureExecuteNonQuery("pro_jugdeCourseUpdate", SqlPArry3);

                if (res!=0)
                {
                    Label2.Text = "选课成功!";
                    string sql = "update course set cCount=cCount-1 where cproperties='选修'and cno=" + cno;
                    DBAccess.GetState(sql);
                    Repeater1.DataBind();
                    //return;
                    //Response.Write("<script>alert('选课成功!');</srcipt>");
                    return;
                    //Repeater1.DataBind();
                   // return;
                    
                }
                else
                {
                    Label2.Text = "选课失败!";
                    Repeater1.DataBind();
                   //Response.Write("<script>alert('选课失败!');</srcipt>");
                    //Repeater1.DataBind();
                    return;
                }


            }


            if (e.CommandName == "cancelCourse")
            {
                string sql = "select count(*) from grade g join course c on g.cno=c.cno where cproperties='选修' and status=1  and g.stuno=@stuno and g.cno=@cno";
                SqlParameter[] SqlArry = { new SqlParameter("@stuno", username.Text),
                                          new SqlParameter("@cno", cno) };
                object res=DBAccess.GetOne(sql,SqlArry);
                if ((int)res!=0)
                {

                    string sqlCancel = "UPDATE dbo.grade SET status=0 WHERE  stuno=@stuno and cno=@cno";
                    SqlParameter[] cancelArray = { new SqlParameter("@stuno", username.Text),
                                          new SqlParameter("@cno", cno) };
                    int resCancel = DBAccess.GetState(sqlCancel, cancelArray);
                    if (resCancel != 0)
                    {
                        Label2.Text = "取消课程成功!";
                        string sql2 = "update course set cCount=cCount+1 where cproperties='选修'and cno=" + cno;
                        DBAccess.GetState(sql2);
                        Repeater1.DataBind();
                    }
                    else
                    {
                        Label2.Text = "取消失败";
                        return;
                    }

                }
                else
                {
                    Label2.Text = "没有选课时请不要乱点!";
                    return;
                }
                

            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string sql = "select * from course where cproperties='选修'";
            DBAccess.GetOne(sql);
            Repeater1.DataBind();
        }
    }
}